<template>
	<view class="class-time-container">
		<cc-navbar :title="navbarTitle"></cc-navbar>
		<u-sticky>
			<view class="class-time-header">
				<u-row gutter="12">
					<u-col span="4">
						<cc-date-picker :value.sync="query.date_range" @change="getList(1)"></cc-date-picker>
					</u-col>
					<u-col span="4">
						<cc-class-select :value.sync="query.cla_id" @complete="getList(1)" />
					</u-col>
					<u-col span="4">
						<cc-class-status-select :value.sync="query.status" @complete="getList(1)" />
					</u-col>
				</u-row>
			</view>
		</u-sticky>
		<view class="list" v-if="count">
			<block v-for="(item, index) in list" :key="index">
				<cc-classtime :item="item" @complete="getList(1)"></cc-classtime>
			</block>
		</view>
		<cc-loadmore :showLimit="4" :loading="loading" :current="list.length" :count="count" />
		<cc-empty v-if="!loading && !count" mode="list" />
	</view>
</template>

<script>
	import {
		classCourseList
	} from '@/codecat/api/education/class/classTime';
	export default {
		name: 'ClassTime',
		data() {
			const endInitDate = this.$dayjs().format('YYYY-MM-DD');
			const beginInitDate = this.$dayjs().subtract(1, 'w').format('YYYY-MM-DD');
			return {
				navbarTitle: '',
				loading: false,
				list: [],
				count: 0,
				query: {
					cla_id: null,
					date_range: [beginInitDate, endInitDate],
					status: null,
					page: 1,
					limit: 10
				}
			};
		},
		onLoad() {
			const options = this.$Route.query;
			if (options.cla_id) {
				this.query.cla_id = options.cla_id
				this.getList(1);
			}
		},
		onReachBottom() {
			if (this.list.length < this.count) {
				this.getList();
			}
		},
		methods: {
			getList(first) {
				const query = this.$lodash.cloneDeep(this.query);
				if (first) {
					query.page = 1;
					this.list = [];
					this.count = 0;
				} else {
					query.page += 1;
				}
				this.loading = true;
				classCourseList(query)
					.then((res) => {
						this.loading = false;
						this.query.page = query.page;
						const {
							list,
							count
						} = res.data;
						this.count = count || 0;
						if (Array.isArray(list) && list.length) {
							this.list = this.list.concat(list);
							if (query.page === 1) {
								this.navbarTitle = `${this.list[0].class_name}-课程表`;
							}
						}
					})
					.catch((_) => {
						this.loading = false;
					});
			}
		}
	};
</script>

<style lang="scss" scoped>
	@import './index.scss';
</style>